﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;

namespace ProjetSMA.Model
{

    public class Element
    {
        protected List<Element> m_neighbors = new List<Element>();
        protected List<ItemMove> m_occupants = new List<ItemMove>();
        protected Element m_north_west;
        protected Element m_north;
        protected Element m_north_east;
        protected Element m_east;
        protected Element m_south_east;
        protected Element m_south;
        protected Element m_south_west;
        protected Element m_west;

        protected int m_i;
        protected int m_j;

        protected int m_height = 1;
        protected Food m_food = null;

        protected string m_name = "_default";
        protected int m_time = 5000;

        public Element(int i, int j, string par_name)
        {
            m_name = par_name;
            m_i = i;
            m_j = j;

        }

        public void InitialisationNeighbors(Element[,] par_map_world, int par_l)
        {
            m_north_west = TestItem(m_i - 1, m_j - 1, par_map_world, par_l);
            m_north = TestItem(m_i, m_j - 1, par_map_world, par_l);
            m_north_east = TestItem(m_i + 1, m_j - 1, par_map_world, par_l);
            m_east = TestItem(m_i + 1, m_j, par_map_world, par_l);

            m_south_east = TestItem(m_i + 1, m_j + 1, par_map_world, par_l);
            m_south = TestItem(m_i, m_j + 1, par_map_world, par_l);
            m_south_west = TestItem(m_i - 1, m_j + 1, par_map_world, par_l);
            m_west = TestItem(m_i - 1, m_j, par_map_world, par_l);

            if (m_north_west != null)
                m_neighbors.Add(m_north_west);
            if (m_north != null)
                m_neighbors.Add(m_north);
            if (m_north_east != null)
                m_neighbors.Add(m_north_east);
            if (m_east != null)
                m_neighbors.Add(m_east);

            if (m_south_east != null)
                m_neighbors.Add(m_south_east);
            if (m_south != null)
                m_neighbors.Add(m_south);
            if (m_south_west != null)
                m_neighbors.Add(m_south_west);
            if (m_west != null)
                m_neighbors.Add(m_west);

        }


        private Element TestItem(int i, int j, Element[,] par_map_world, int par_l)
        {
            if (i < par_l &&
                 j < par_l &&
                 i >= 0 &&
                 j >= 0)
                return par_map_world[i, j];
            return null;
        }


        public void InitialisationHeight()
        {
            if (m_height > 1)
            {

                for (int i = 0; i < m_neighbors.Count; i++)
                {
                    if (m_neighbors[i].Height < m_height)
                    {
                        m_neighbors[i].Height = m_height - 1;
                        m_neighbors[i].InitialisationHeight();
                    }
                }
            }
        }

        public virtual void InitialisationTimer()
        {

        }

        public int I
        {
            get { return m_i; }
            set { m_i = value; }
        }

        public int J
        {
            get { return m_j; }
            set { m_j = value; }
        }

        public Element NorthWest
        {
            get { return m_north_west; }
            set { m_north_west = value; }
        }

        public Element North
        {
            get { return m_north; }
            set { m_north = value; }
        }

        public Element NorthEast
        {
            get { return m_north_east; }
            set { m_north_east = value; }
        }

        public Element East
        {
            get { return m_east; }
            set { m_east = value; }
        }

        public Element SouthEast
        {
            get { return m_south_east; }
            set { m_south_east = value; }
        }

        public Element South
        {
            get { return m_south; }
            set { m_south = value; }
        }

        public Element SouthWest
        {
            get { return m_south_west; }
            set { m_south_west = value; }
        }

        public Element West
        {
            get { return m_west; }
            set { m_west = value; }
        }

        public List<Element> Neighboors
        {
            get { return m_neighbors; }
            set { m_neighbors = value; }
        }

        public List<ItemMove> Occupants
        {
            get { return m_occupants; }
            set { m_occupants = value; }
        }

        public int Height
        {
            get { return m_height; }
            set { m_height = value; }
        }

        public string Name
        {
            get { return m_name; }
            set { m_name = value; }
        }

        public Food Food
        {
            get { return m_food; }
            set { m_food = value; }
        }

        public int Time
        {
            get { return m_time; }
            set { m_time = value; }
        }
    }
}
